package cn.uin.domain.auth.service.user.login.handle;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName CustomLogoutSuccessHandler
 * @Description:退出登陆回调
 * @Author: Administrator
 * @CreateDate: 2025/10/24 16:23
 * @Version: 1.0
 */
@Slf4j
@Component
public class CustomLogoutSuccessHandler implements LogoutSuccessHandler {
	@Override
	public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
		// 构建返回对象
		Map<String, Object> resultMap = new HashMap<>(3);
		resultMap.put("code", HttpServletResponse.SC_OK);
		resultMap.put("message", "退出成功");

		response.setStatus(HttpServletResponse.SC_OK);
		response.setContentType("application/json;charset=utf-8");
		PrintWriter writer = response.getWriter();
		writer.write(new ObjectMapper().writeValueAsString(resultMap));
	}
}
